package cn.edu.tju.core.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import org.hibernate.annotations.BatchSize;

//import javax.persistence.*;
//import javax.validation.constraints.NotNull;
//import javax.validation.constraints.Size;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

@Entity
@Table(name = "users")
@Inheritance(strategy = InheritanceType.JOINED)
public class User extends BaseEntity{

//   @JsonIgnore
//   @Id
//   @Column(name = "ID")
//   @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "USER_SEQ")
//   @SequenceGenerator(name = "USER_SEQ", sequenceName = "USER_SEQ", allocationSize = 1)
//   @GeneratedValue(strategy = GenerationType.IDENTITY)
//   private Long id;

   @Column(name = "USERNAME", length = 100, unique = true)
   @NotNull
   @Size(min = 1, max = 100)
   private String username;

   @JsonIgnore
   @Column(name = "PASSWORD", length = 100)
   @NotNull
   @Size(min = 1, max = 100)
   private String password;

//   @Column(name = "FIRSTNAME", length = 50)
//   @NotNull
//   @Size(min = 4, max = 50)
//   private String firstname;
//
//   @Column(name = "LASTNAME", length = 50)
//   @NotNull
//   @Size(min = 4, max = 50)
//   private String lastname;
//
//   @Column(name = "EMAIL", length = 50)
//   @NotNull
//   @Size(min = 4, max = 50)
//   private String email;

   @JsonIgnore
   @Column(name = "ACTIVATED")
   @NotNull
   private boolean activated;

   @Column(name = "VIP_LEVEL", length = 50)
   private String vipLevel;  // VIP等级名称

   @Column(name = "VIP_LEVEL_VALUE", precision = 5)
   private Integer vipLevelValue = 0;  // VIP等级数值（0=普通用户，1=青铜，2=白银，3=黄金，4=铂金，5=钻石）

   @Column(name = "TOTAL_CONSUMPTION", precision = 19, scale = 2)
   private java.math.BigDecimal totalConsumption = java.math.BigDecimal.ZERO;  // 累计消费金额

   @Column(name = "TOTAL_RECHARGE", precision = 19, scale = 2)
   private java.math.BigDecimal totalRecharge = java.math.BigDecimal.ZERO;  // 累计充值金额

   @ManyToMany
   @JoinTable(
      name = "USER_AUTHORITY",
      joinColumns = {@JoinColumn(name = "USER_ID", referencedColumnName = "ID")},
      inverseJoinColumns = {@JoinColumn(name = "AUTHORITY_NAME", referencedColumnName = "NAME")})
   @BatchSize(size = 20)
   private Set<Authority> authorities = new HashSet<>();

   public String getUsername() {
      return username;
   }

   public void setUsername(String username) {
      this.username = username;
   }

   public String getPassword() {
      return password;
   }

   public void setPassword(String password) {
      this.password = password;
   }

   public boolean isActivated() {
      return activated;
   }

   public void setActivated(boolean activated) {
      this.activated = activated;
   }

   public String getVipLevel() {
      return vipLevel;
   }

   public void setVipLevel(String vipLevel) {
      this.vipLevel = vipLevel;
   }

   public Integer getVipLevelValue() {
      return vipLevelValue;
   }

   public void setVipLevelValue(Integer vipLevelValue) {
      this.vipLevelValue = vipLevelValue;
   }

   public java.math.BigDecimal getTotalConsumption() {
      return totalConsumption;
   }

   public void setTotalConsumption(java.math.BigDecimal totalConsumption) {
      this.totalConsumption = totalConsumption;
   }

   public java.math.BigDecimal getTotalRecharge() {
      return totalRecharge;
   }

   public void setTotalRecharge(java.math.BigDecimal totalRecharge) {
      this.totalRecharge = totalRecharge;
   }

   public Set<Authority> getAuthorities() {
      return authorities;
   }

   public void setAuthorities(Set<Authority> authorities) {
      this.authorities = authorities;
   }

   /**
    * 增加累计消费金额
    * @param amount 消费金额
    */
   public void addConsumption(java.math.BigDecimal amount) {
      if (amount == null || amount.compareTo(java.math.BigDecimal.ZERO) <= 0) return;
      if (this.totalConsumption == null) {
         this.totalConsumption = java.math.BigDecimal.ZERO;
      }
      this.totalConsumption = this.totalConsumption.add(amount);
   }

   /**
    * 增加累计充值金额
    * @param amount 充值金额
    */
   public void addRecharge(java.math.BigDecimal amount) {
      if (amount == null || amount.compareTo(java.math.BigDecimal.ZERO) <= 0) return;
      if (this.totalRecharge == null) {
         this.totalRecharge = java.math.BigDecimal.ZERO;
      }
      this.totalRecharge = this.totalRecharge.add(amount);
   }

   /**
    * 设置VIP等级
    * @param vipLevelName VIP等级名称
    * @param vipLevelValue VIP等级数值
    */
   public void setVipLevel(String vipLevelName, Integer vipLevelValue) {
      this.vipLevel = vipLevelName;
      this.vipLevelValue = vipLevelValue;
   }

   @Override
   public boolean equals(Object o) {
      if (this == o) return true;
      if (o == null || getClass() != o.getClass()) return false;
      User user = (User) o;
      return this.getId().equals(user.getId());
   }

   @Override
   public int hashCode() {
      return Objects.hash(this.getId());
   }

   @Override
   public String toString() {
      return "User{" +
         "username='" + username + '\'' +
         ", password='" + password + '\'' +
         ", activated=" + activated +
         ", vipLevel='" + vipLevel + '\'' +
         ", vipLevelValue=" + vipLevelValue +
         '}';
   }
}
